﻿Option Strict On
Option Infer On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.StoredProcModel
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Public Class EntityData
   Inherits BaseData
   Implements IEntityData

   'Private Shared mIsDataLoaded As Boolean
   Private mRecordSet As IRecordsetData
   Private mSetRecordSet As IRecordsetData
   Private mChildren As IEnumerable(Of IChildData)

   Public Shared Function Load(ByVal rootData As RootData, ByVal xElement As XElement) _
            As EntityData
      Dim ret = rootData.GetEntity(xElement.@Name)
      If ret Is Nothing Then
         ret = LoadElement(rootData, xElement)
      End If
      Return ret
   End Function

   Friend Shared Function LoadElement( _
            ByVal rootdata As RootData, _
            ByVal element As XElement) _
            As EntityData
      Return New EntityData(rootdata, element)
   End Function

   Private Sub New( _
           ByVal rootdata As RootData, _
           ByVal element As XElement)
      MyBase.New(rootdata, element)
      Me.mRecordSet = RecordsetData.Load(rootdata, (From x In element...<orm:BuildRecordset>).FirstOrDefault)
      Me.mSetRecordSet = RecordsetData.Load(rootdata, (From x In element...<orm:SetSelect>).FirstOrDefault)
      Me.mChildren = ChildData.Load(rootdata, Me, (From x In element...<orm:BuildRecordset> Skip 1))
   End Sub

#Region "IEntityData Implementation"
   Public ReadOnly Property DoGenerateCreate() As Boolean Implements IEntityData.DoGenerateCreate
      Get
         Return Me.Element.@Generate.Contains("C")
      End Get
   End Property

   Public ReadOnly Property DoGenerateDelete() As Boolean Implements IEntityData.DoGenerateDelete
      Get
         Return Me.Element.@Generate.Contains("D")
      End Get
   End Property

   Public ReadOnly Property DoGenerateRetrieve() As Boolean Implements IEntityData.DoGenerateRetrieve
      Get
         Return Me.Element.@Generate.Contains("R")
      End Get
   End Property

   Public ReadOnly Property DoGenerateSetSelect() As Boolean Implements IEntityData.DoGenerateSetSelect
      Get
         Return Me.Element.@Generate.Contains("U")
      End Get
   End Property

   Public ReadOnly Property DoGenerateUpdate() As Boolean Implements IEntityData.DoGenerateUpdate
      Get
         Return Me.Element.@Generate.Contains("S")
      End Get
   End Property

   Public ReadOnly Property Name() As String Implements IEntityData.Name
      Get
         Return Me.Element.@Name
      End Get
   End Property

   Public ReadOnly Property ObjectName() As String Implements IEntityData.ObjectName
      Get
         Return Me.Element.@ObjectName
      End Get
   End Property

   Public ReadOnly Property Recordset() _
            As IRecordsetData _
            Implements IEntityData.Recordset
      Get
         Return mRecordSet
      End Get
   End Property

   Public ReadOnly Property SetRecordset() _
            As IRecordsetData _
            Implements IEntityData.SetRecordset
      Get
         Return mSetRecordSet
      End Get
   End Property
#End Region

   Public ReadOnly Property Children() _
            As IEnumerable(Of IChildData) _
            Implements StoredProcModel.IEntityData.Children
      Get
         Return mChildren
      End Get
   End Property
End Class
